<?php
/**
 * Created by PhpStorm.
 * User: song
 * Date: 2018/8/15
 * Time: 上午10:54
 */


namespace app\model;


class RoomModel extends BaseModel
{

    public static function getRoomInfo($roomNum)
    {

        //查询具体数据,并刷新缓存
        $result = self::getListByWhere(['room_num' => (string)$roomNum]);


        return $result[0] ?? false;

    }

    /**
     * @param $where
     * @param string $limit
     * @param array $otherParam
     * @return mixed
     */
    private static function getListByWhere($where, $limit = '', array $otherParam = [])
    {
        $db = new DbModel($_SERVER['DB_CONFIG']['xiaoyu']);

        $field = $otherParam['field'] ?? '*';
        $group = $otherParam['group'] ?? '';
        $order = $otherParam['order'] ?? 'id desc';

        $db = $db->table('xyu_room');

        if (!empty($limit)) {
            $db->limit($limit);
        }

        if ($group) {
            $db->group($group);
        }

        $result = $db->where($where)->order($order)->select($field);


        return $result;
    }

    public static function getListByWhereAll($where)
    {

        $list = self::getListByWhere($where, 1);

        return $list;
    }

    /**
     * 获取单条记录
     * @param int $id
     * @return array
     */
    public static function getInfo(int $id)
    {
        $where = ['id' => $id];

        $list = self::getListByWhere($where);

        return $list[0] ?? [];
    }

    /**
     * 内部方法，更新数据
     * @param array $where
     * @param array $data
     * @return mixed
     */
    private static function updateByWhere(array $where, array $data)
    {
        $roomApi = new DbModel();

        //更新条件
        $roomApi = $roomApi->table('xyu_room')->where($where);

        //执行更新并返回数据
        $roomApi->update($data);
    }

    /**
     * 更新生成聊天记录状态
     * @param string $roomNum
     * @param int $status
     */
    public static function updateChatStatus(string $roomNum, int $status)
    {
        $where = ['room_num' => $roomNum];
        $data = ['chat_status' => $status];
        self::updateByWhere($where, $data);
    }
}
